package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ApplicationContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.enity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //获取当前登录用户，为购物车对象赋值
        //获取当前用户对象
        Long currentId = (Long) ApplicationContext.getData();
        //设置购物车用户Id 将获取到的值赋值过来
        shoppingCart.setUserId(currentId);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        // UserId = 获取到的当前用户
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            //添加到购物车的是菜品
            //DishId = dishId
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            //DishId = dishId
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?

        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne != null) {
            //如果已经存在，在原来数量上加一
            //获取当前的个数
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //如果不存在，则添加购物车，默认数量是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //UserId = getData
        queryWrapper.eq(ShoppingCart::getUserId, ApplicationContext.getData());
        //获取时间来进行排序
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        //调用shoppingCartService的list方法将queryWrapper条件传进去创造一个list集合
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        //将list集合返回
        return R.success(list);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        //菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long userId = (Long) ApplicationContext.getData();
        queryWrapper.eq(ShoppingCart::getUserId, userId);//where user_id =?
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {//是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);// dish_id = ?
        } else {// 是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());// setmeal_id = ?
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        // number -1 然后操作：
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number > 0) {
            // >0  update更新 update set number=? where id = ?
            shoppingCartService.updateById(one);
        } else {
            // <= 0  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.remove(queryWrapper);
        }
        return R.success(one);
    }
}
